package com.chanyachting.emall.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.chanyachting.emall.domain.Channel;

public class PageUtil {
	
	public static String getFy3(int pageCount,int currentPage,String toPageAct) {
		//link内容
		String linkStr = "";
		
		int startId = 0;
		int endId = 0;
		if (currentPage % 10 == 0) {
			startId = currentPage - (currentPage -1) % 10;
			endId = currentPage - (currentPage - 1) % 10 + 9;
		} else {
			startId = currentPage - currentPage%10 + 1;
			endId = currentPage - currentPage%10 + 10;
		}
		
		if (pageCount % 10 != 0 && startId == (pageCount - pageCount % 10 + 1)) {
			endId = startId + pageCount % 10 - 1;
		}
		
		//只有一页的情况，就不显示翻页link
		if (pageCount == 0 || pageCount < 10 && startId == endId) {
			return "";
		}
		
		if (currentPage > 10 || currentPage > startId) {
			linkStr = "<a href=\"" + toPageAct + "&currentPage=" + (currentPage - 1) + "\" class=\"pre\">上一页</a>";
		}

		if (currentPage % 10 == 0 && currentPage != pageCount) {
			startId ++;
		}

		for(int i=startId ; i <= endId; i++) {
			if(i == currentPage){
				linkStr = linkStr + "<a href=\"" + toPageAct + "&currentPage=" + i + "\" class=\"current\">"+i+"</a>";
			}else{
				linkStr = linkStr + "<a href=\"" + toPageAct + "&currentPage=" + i + "\">"+i+"</a>";
			}
		}
		if (currentPage % 10 == 0 && currentPage != pageCount) {
			linkStr = linkStr + "<a href=\"" + toPageAct + "&currentPage=" + (currentPage + 1) + "\">"+(currentPage + 1)+"</a>";
		}
		if (currentPage < pageCount) {
			linkStr = linkStr +  "<a href=\"" + toPageAct + "&currentPage=" + (currentPage + 1) + "\" class=\"next\">下一页</a>";
		}
		
		return linkStr;
	}
	
	public static String getFy4(int pageCount,int currentPage,String toPageAct) {
		//selection内容
		String selectionStr = "";
		
		int startId = 0;
		int endId = 0;
		if (currentPage % 20 == 0) {
			startId = currentPage - (currentPage -1) % 20;
			endId = currentPage - (currentPage - 1) % 20 + 19;
		} else {
			startId = currentPage - currentPage%20 + 1;
			endId = currentPage - currentPage%20 + 20;
		}
		
		if (pageCount % 20 != 0 && startId == (pageCount - pageCount % 20 + 1)) {
			endId = startId + pageCount % 20 - 1;
		}
		
		//只有一页的情况，就不显示翻页selection
		if (pageCount == 0 || pageCount < 20 && startId == endId) {
			return "";
		}
		
		if (currentPage > 20 || currentPage > startId) {
			selectionStr = "<input type=\"image\" src=\"../images/q.gif\" class=\"qh\" name=\"button5\" id=\"button5\" value=\"&lt;\" onclick=\"gotoPage('" + (currentPage - 1) + "');\"/>";
		}

		if (currentPage % 20 == 0 && currentPage != pageCount) {
			startId ++;
		}
		
		selectionStr = selectionStr + "<div id=\"test6\" class=\"dropdown\" tabIndex=\"6\">";
		selectionStr = selectionStr + "<select name=\"select6\"  onchange=\"gotoPage2();\">";
		for(int i=startId ; i <= endId; i++) {
			String pageNumber = fillZero(String.valueOf(i),String.valueOf(pageCount),"1");
			if(i == currentPage){
				selectionStr = selectionStr + "<option selected=\"selected\" value=\"" + i + "\">" + pageNumber + "/" + pageCount + "</option>";
			}else{
				selectionStr = selectionStr + "<option value=\"" + i + "\">" + pageNumber + "/" + pageCount + "</option>";
			}
		}
		if (currentPage % 10 == 0 && currentPage != pageCount) {
			selectionStr = selectionStr + "<option value=\"" + currentPage + "\">" + fillZero(String.valueOf(currentPage),String.valueOf(pageCount),"1") + "/" + pageCount + "</option>";
		}
		selectionStr = selectionStr + "</select>";
		selectionStr = selectionStr + "</div>";
		if (currentPage < pageCount) {
			selectionStr = selectionStr +  "<input type=\"image\" src=\"../images/h.gif\" class=\"qh\" name=\"button6\" id=\"button6\" value=\"&gt;\" onclick=\"gotoPage('" + (currentPage + 1) + "');\" />";
		}
		
		return selectionStr;
	}
	
	public static String getGotoAction(String urlStr,Map params) {
		int ids = urlStr.lastIndexOf("/");
		String tmpStr = urlStr.substring(ids + 1);
		Iterator iter = params.entrySet().iterator();
		int paramCnt = 0;
		while (iter.hasNext()) {
			Map.Entry entry  = (Map.Entry)iter.next();
			String key = entry.getKey().toString();
			
			if ("currentPage".equals(key)) {
				continue;
			}
			
			String val[] = (String[]) entry.getValue();
			if (paramCnt == 0) {
				tmpStr = tmpStr + "?";
			}
			tmpStr = tmpStr + "&" + key + "=" + val[0];
			paramCnt ++;
		}
		
		return tmpStr;
	}
	
	/*
	 * fillTarget 
	 * 1:左添加“0”
	 * 2:右添加“0”
	 */
	public static String fillZero(String str,String str2,String fillTarget) {
		int len1 = str.length();
		int len2 = str2.length();
		
		if (len1 == len2) {
			return str;
		}
		for (int i=len1;i<len2;i++) {
			if ("1".equals(fillTarget)) {
				str = "0" + str;
			} else {
				str = str + "0";
			}
		}
		
		return str;
	}
	
	public static String getSubChannelName(List<Channel> cList,Integer subChannelId) {
		String tmpStr = "";
		for (Channel c:cList) {
			if (subChannelId.equals(c.getId())) {
				tmpStr = c.getName();
				break;
			}
		}
		return tmpStr;
	}
}
